package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.InfoContentDao;
import com.gusha.model.InfoContent;
import com.gusha.model.User;


/**
 * @Created：2018-10-30 14:25:23
 * @Author LightSound的代码生成器
 * @Description:InfoContentService服务类
 */

@Service
@Transactional
public class InfoContentService {

    @Autowired
    private InfoContentDao infoContentDao;

    public Page<InfoContent> findAll(Pageable pageable, InfoContent infoContent) {
        @SuppressWarnings("serial")
        Page<InfoContent> result = infoContentDao.findAll(new Specification<InfoContent>() {
            @Override
            public Predicate toPredicate(Root<InfoContent> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (infoContent.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),infoContent.getId()));
                }
                if (infoContent.getClassifyId() != null) {
                    list.add(cb.equal(root.get("classifyId").as(BigInteger.class),infoContent.getClassifyId()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoTitle())) {
                    list.add(cb.like(root.get("infoTitle").as(String.class),"%"+infoContent.getInfoTitle()+"%"));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoIcon())) {
                    list.add(cb.equal(root.get("infoIcon").as(String.class),infoContent.getInfoIcon()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoRemark())) {
                    list.add(cb.equal(root.get("infoRemark").as(String.class),infoContent.getInfoRemark()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoDesc())) {
                    list.add(cb.equal(root.get("infoDesc").as(String.class),infoContent.getInfoDesc()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoContents())) {
                    list.add(cb.equal(root.get("infoContents").as(String.class),infoContent.getInfoContents()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoKey())) {
                    list.add(cb.equal(root.get("infoKey").as(String.class),infoContent.getInfoKey()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoAuthor())) {
                    list.add(cb.equal(root.get("infoAuthor").as(String.class),infoContent.getInfoAuthor()));
                }
                if (StringUtils.isNotBlank(infoContent.getInfoSource())) {
                    list.add(cb.equal(root.get("infoSource").as(String.class),infoContent.getInfoSource()));
                }
                if (infoContent.getClickNum() != null) {
                    list.add(cb.equal(root.get("clickNum").as(Long.class),infoContent.getClickNum()));
                }
                if (infoContent.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(Integer.class),infoContent.getIsDelete()));
                }
                if (infoContent.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),infoContent.getCreateTime()));
                }
                if (infoContent.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),infoContent.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(infoContent.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),infoContent.getCreator()));
                }
                if (StringUtils.isNotBlank(infoContent.getUpdator())) {
                    list.add(cb.equal(root.get("updator").as(String.class),infoContent.getUpdator()));
                }
                if (infoContent.getUserId() !=null) {
                    list.add(cb.equal(root.get("userId").as(User.class),infoContent.getUserId()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
